package ks.server.controllers.requests;

import java.util.Collection;

import org.w3c.dom.Document;

import ks.framework.common.Message;
import ks.framework.communicator.Communicator;
import ks.managers.plugin.Plugin;

public class PluginsRequest {
	private Communicator com;
	
	public PluginsRequest(Communicator c) {
		this.com = c;
	}
	
	public boolean send(Collection<Plugin> plugins) {
		StringBuilder sb = new StringBuilder("<request version=\"1.0\" id=\"12345\">");
		sb.append("<plugins>");
		for (Plugin plugin : plugins)
			sb.append("<plugin-installed name=\"" + plugin.getName() + "\" losses=\"" + plugin.getLosses() + "\" wins=\"" + plugin.getWins() + "\" date=\"" + plugin.getDate() + "\" suspended=\"" + plugin.getSuspended() + "\"/>");
		sb.append("</plugins>");
		sb.append("</request>");
		
		Document d = Message.construct(sb.toString());
		Message r = new Message(d);
		r.setBroadcast();
		return com.distribute(r);
	}
	
	public boolean send(Collection<Plugin> plugins, Collection<String> players) {
		StringBuilder sb = new StringBuilder("<request version=\"1.0\" id=\"12345\">");
		sb.append("<plugins>");
		for (Plugin plugin : plugins)
			sb.append("<plugin-installed name=\"" + plugin.getName() + "\" losses=\"" + plugin.getLosses() + "\" wins=\"" + plugin.getWins() + "\" date=\"" + plugin.getDate() + "\" suspended=\"" + plugin.getSuspended() + "\"/>");
		sb.append("</plugins>");
		sb.append("</request>");
		
		Document d = Message.construct(sb.toString());
		Message r = new Message(d);
		return com.distribute(players.iterator(), r);
	}
}